package com.kly.cashmall.popup;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;

import com.kly.cashmall.R;
import com.kly.cashmall.base.app_action.ActionHeads;
import com.kly.cashmall.base.app_action.BaseActionHelper;

import razerdp.basepopup.BasePopupWindow;

/**
 * @author
 * @date 2017/9/4上午11:02
 */
public class UpdatePopupWindow extends BasePopupWindow {
    private final CharSequence title;
    private final CharSequence content;
    private final Context context;
    private Callback callback;

    private TextView btnOk;
    private ImageView imageView;
    private ImageView btnClose;
    private TextView txtContent;

    public UpdatePopupWindow(@NonNull Context context, CharSequence title, CharSequence content, Callback callback) {
        super(context);
        setContentView(R.layout.popup_update);
        this.context = context;
        this.callback = callback;
        this.title = title;
        this.content = content;
        initView();
        //初始化界面控件的事件
        initEvent();
    }


    private void initView() {
        TextView txtTitle = findViewById(R.id.txt_title);
        txtContent = findViewById(R.id.txt_content);
        btnOk = findViewById(R.id.btn_ok);
        btnClose = findViewById(R.id.img_close);
        imageView = findViewById(R.id.img_title_icon);
        txtTitle.setText(title);
        txtContent.setText(content);
    }

    public void setCloseButton(boolean show) {
        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (show) {
                    BaseActionHelper.with(context).handleAction(ActionHeads.LMActionFinish);
                }
                dismiss();
            }
        });
    }

    public void setIcon(@DrawableRes int resId) {
        imageView.setImageResource(resId);
    }

    public void setTextGravity(int gravity) {
        txtContent.setGravity(gravity);
    }


    public void setBtnText(String text) {
        btnOk.setText(text);
    }

    private void initEvent() {
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.onCallBack(UpdatePopupWindow.this);
            }
        });
    }

    public interface Callback {
        void onCallBack(UpdatePopupWindow generalOneDialog);
    }
}
